package com.bibrApp.difu.api;


import android.app.Activity;

import com.bibrApp.difu.DfApp;
import com.bibrApp.difu.utils.AppManager;

import java.io.IOException;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @CreateDate 2020/2/25
 *
 *  @Author dj
 */
public class HeaderInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Activity activity = AppManager.getTopActivity();
        DfApp dfApp = (DfApp) activity.getApplication();
        String userId = dfApp.getUserId();
        String token = dfApp.getToken();
        HttpUrl url = request.url();
        Request.Builder requestBuilder = request.newBuilder();
        if (url.toString().contains("strict")) {
            requestBuilder = requestBuilder.addHeader("userId", userId)
                    .addHeader("Authorization", "Bearer " + token);
        }
        Request build = requestBuilder.method(request.method(), request.body())
                .addHeader("deviceType", "app")
                .addHeader("Content-Type", "application/json;charset=utf-8")
                .build();
        return chain.proceed(build);
    }
}